package com.serverGroup.baseModule.controller;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.serverGroup.baseModule.service.UserService;

@Controller
@RequestMapping("/user")
public class UserAction {

	@Autowired
	private UserService userService;

	@RequestMapping(value = "/login", method = RequestMethod.POST, produces = "application/json; charset=UTF-8")
	public void login(@RequestParam String username, @RequestParam String password) {
		Subject subject = SecurityUtils.getSubject();
		UsernamePasswordToken token = new UsernamePasswordToken(username, password);
		try {
			// 执行认证操作.
			
			Session session = subject.getSession(true);
			
			if (session.getAttribute(username) != null) {
				
			}else {
				subject.login(token);
			}
		} catch (UnknownAccountException e) {
			// TODO: handle exception
			System.out.println("用户不存在");
		} catch (IncorrectCredentialsException e) {
			// TODO: handle exception
			System.out.println("密码错误");
		} catch (AuthenticationException ae) {
			System.out.println("登陆失败: " + ae.getMessage());
		}
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

}
